All files / model mutation_batch.ts

100% Statements 60/60
91.67% Branches 11/12
100% Functions 11/11
100% Lines 57/57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206                                      2x                 2x 2x   2x         2x   1798x 1798x 1798x                       2x         448x 69x             448x 448x             448x 458x 458x 458x 458x     448x                   2x       904x 209x           904x   904x 1080x 1080x 962x             904x     2x 1180x   1270x 1270x   1180x     2x 240x                           2x 1373x       2x 16x   2x     2x   532x 532x 532x 532x         532x               2x           532x               532x 532x 532x 562x 562x     4x     562x     532x               2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { SnapshotVersion } from '../core/snapshot_version';
import { Timestamp } from '../core/timestamp';
import { BatchId, ProtoByteString } from '../core/types';
import {
  documentKeySet,
  DocumentKeySet,
  DocumentVersionMap,
  documentVersionMap
} from './collections';
import { MaybeDocument } from './document';
import { DocumentKey } from './document_key';
import { Mutation, MutationResult } from './mutation';
import { assert } from '../util/assert';
import * as misc from '../util/misc';
 
export const BATCHID_UNKNOWN = -1;
 
/**
 * A batch of mutations that will be sent as one unit to the backend.
 */
export class MutationBatch {
  constructor(
    public batchId: BatchId,
    public localWriteTime: Timestamp,
    public mutations: Mutation[]
  ) {}
 
  /**
   * Applies all the mutations in this MutationBatch to the specified document
   * to create a new remote document
   *
   * @param docKey The key of the document to apply mutations to.
   * @param maybeDoc The document to apply mutations to.
   * @param batchResult The result of applying the MutationBatch to the
   * backend.
   */
  applyToRemoteDocument(
    docKey: DocumentKey,
    maybeDoc: MaybeDocument | null,
    batchResult: MutationBatchResult
  ): MaybeDocument | null {
    if (maybeDoc) {
      assert(
        maybeDoc.key.isEqual(docKey),
        `applyToRemoteDocument: key ${docKey} should match maybeDoc key
        ${maybeDoc.key}`
      );
    }
 
    const mutationResults = batchResult.mutationResults;
    assert(
      mutationResults.length === this.mutations.length,
      `Mismatch between mutations length
      (${this.mutations.length}) and mutation results length
      (${mutationResults.length}).`
    );
 
    for (let i = 0; i < this.mutations.length; i++) {
      const mutation = this.mutations[i];
      Eif (mutation.key.isEqual(docKey)) {
        const mutationResult = mutationResults[i];
        maybeDoc = mutation.applyToRemoteDocument(maybeDoc, mutationResult);
      }
    }
    return maybeDoc;
  }
 
  /**
   * Computes the local view of a document given all the mutations in this
   * batch.
   *
   * @param docKey The key of the document to apply mutations to.
   * @param maybeDoc The document to apply mutations to.
   */
  applyToLocalView(
    docKey: DocumentKey,
    maybeDoc: MaybeDocument | null
  ): MaybeDocument | null {
    if (maybeDoc) {
      assert(
        maybeDoc.key.isEqual(docKey),
        `applyToLocalDocument: key ${docKey} should match maybeDoc key
        ${maybeDoc.key}`
      );
    }
    const baseDoc = maybeDoc;
 
    for (let i = 0; i < this.mutations.length; i++) {
      const mutation = this.mutations[i];
      if (mutation.key.isEqual(docKey)) {
        maybeDoc = mutation.applyToLocalView(
          maybeDoc,
          baseDoc,
          this.localWriteTime
        );
      }
    }
    return maybeDoc;
  }
 
  keys(): DocumentKeySet {
    let keySet = documentKeySet();
 
    for (const mutation of this.mutations) {
      keySet = keySet.add(mutation.key);
    }
    return keySet;
  }
 
  isEqual(other: MutationBatch): boolean {
    return (
      this.batchId === other.batchId &&
      misc.arrayEquals(this.mutations, other.mutations)
    );
  }
 
  /**
   * Returns true if this mutation batch has already been removed from the
   * mutation queue.
   *
   * Note that not all implementations of the MutationQueue necessarily use
   * tombstones as part of their implementation and generally speaking no code
   * outside the mutation queues should really care about this.
   */
  isTombstone(): boolean {
    return this.mutations.length === 0;
  }
 
  /** Converts this batch into a tombstone */
  toTombstone(): MutationBatch {
    return new MutationBatch(this.batchId, this.localWriteTime, []);
  }
}
 
/** The result of applying a mutation batch to the backend. */
export class MutationBatchResult {
  private constructor(
    readonly batch: MutationBatch,
    readonly commitVersion: SnapshotVersion,
    readonly mutationResults: MutationResult[],
    readonly streamToken: ProtoByteString,
    /**
     * A pre-computed mapping from each mutated document to the resulting
     * version.
     */
    readonly docVersions: DocumentVersionMap
  ) {}
 
  /**
   * Creates a new MutationBatchResult for the given batch and results. There
   * must be one result for each mutation in the batch. This static factory
   * caches a document=>version mapping (docVersions).
   */
  static from(
    batch: MutationBatch,
    commitVersion: SnapshotVersion,
    results: MutationResult[],
    streamToken: ProtoByteString
  ) {
    assert(
      batch.mutations.length === results.length,
      'Mutations sent ' +
        batch.mutations.length +
        ' must equal results received ' +
        results.length
    );
 
    let versionMap = documentVersionMap();
    const mutations = batch.mutations;
    for (let i = 0; i < mutations.length; i++) {
      let version = results[i].version;
      if (version === null) {
        // deletes don't have a version, so we substitute the commitVersion
        // of the entire batch.
        version = commitVersion;
      }
 
      versionMap = versionMap.insert(mutations[i].key, version);
    }
 
    return new MutationBatchResult(
      batch,
      commitVersion,
      results,
      streamToken,
      versionMap
    );
  }
}